package com.ftwj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ftwj.entity.DictInfo;
import com.ftwj.entity.dict.AddDictDTO;
import com.ftwj.entity.dict.DictDTO;
import com.ftwj.service.DictInfoService;
import com.ftwj.mapper.DictInfoMapper;
import com.ftwj.utils.result.MessageEnum;
import com.ftwj.utils.result.ResultBean;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* @author 10572
* @description 针对表【dict_info(字典信息表)】的数据库操作Service实现
* @createDate 2024-06-28 18:08:21
*/
@Service
public class DictInfoServiceImpl extends ServiceImpl<DictInfoMapper, DictInfo>
    implements DictInfoService{


    /**
      * @Author Liu_xg
      * @Date  8:44
      * @Description 字典查询
      */
    @Override
    public ResultBean queryDictList(HttpServletRequest request, HttpServletResponse response, DictDTO dictDTO) {
        LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<DictInfo>()
                .select(DictInfo::getId, DictInfo::getDictType, DictInfo::getDictType, DictInfo::getDictValue)
                .eq(DictInfo::getDictType, dictDTO.getType())
                .eq(DictInfo::getState, 1);
        List<DictInfo> dictInfoList = baseMapper.selectList(lambdaQueryWrapper);
        return ResultBean.success(dictInfoList);
    }


    /**
      * @Author Liu_xg
      * @Date  8:45
      * @Description 新增字典
      */
    @Override
    public ResultBean addDict(HttpServletRequest request, HttpServletResponse response, AddDictDTO addDictDTO) {
        DictInfo dictInfo = new DictInfo();
        dictInfo.setDictType(addDictDTO.getDictType());
        dictInfo.setDictKey(addDictDTO.getDictKey());
        dictInfo.setDictValue(addDictDTO.getDictValue());
        dictInfo.setState(1);

        boolean state = save(dictInfo);
        if (state) {
            return ResultBean.success();
        }
        return ResultBean.error(MessageEnum.FAIIL);
    }


    /**
      * @Author Liu_xg
      * @Date  8:46
      * @Description 编辑字典
      */
    @Override
    public ResultBean editDict(HttpServletRequest request, HttpServletResponse response, AddDictDTO addDictDTO) {
        DictInfo dictInfo = new DictInfo();
        dictInfo.setDictType(addDictDTO.getDictType());
        dictInfo.setDictKey(addDictDTO.getDictKey());
        dictInfo.setDictValue(addDictDTO.getDictValue());
        dictInfo.setId(addDictDTO.getId());

        boolean state = updateById(dictInfo);
        if (state) {
            return ResultBean.success();
        }
        return ResultBean.error(MessageEnum.FAIIL);
    }


    /**
      * @Author Liu_xg
      * @Date  8:47
      * @Description 删除字典
      */
    @Override
    public ResultBean delDict(HttpServletRequest request, HttpServletResponse response, AddDictDTO addDictDTO) {
        DictInfo dictInfo = new DictInfo();
        dictInfo.setId(addDictDTO.getId());
        dictInfo.setState(-1);

        boolean state = updateById(dictInfo);
        if (state) {
            return ResultBean.success();
        }
        return ResultBean.error(MessageEnum.FAIIL);
    }
}




